﻿using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;

namespace Red.Controls.Themes
{
    /// <summary>
    /// Trigger that can set the icon for a <see cref="System.Windows.Controls.MenuItem"/> based on the value of the header
    /// 
    /// Utility class for removing boilerplate bloat
    /// </summary>
    public class IconTrigger : DataTrigger
    {
        public static readonly DependencyProperty CanvasProperty = DependencyProperty.Register( "Canvas", typeof( Canvas ), typeof( IconTrigger ) );
        public static readonly DependencyProperty IconSizeProperty = DependencyProperty.Register( "IconSize", typeof( double ), typeof( IconTrigger ), new PropertyMetadata( 16.0 ) );

        public Canvas Canvas
        {
            get { return (Canvas)GetValue( CanvasProperty ); }
            set { SetValue( CanvasProperty, value ); }
        }

        public double IconSize
        {
            get { return (double)GetValue( IconSizeProperty ); }
            set { SetValue( IconSizeProperty, value ); }
        }

        public IconTrigger()
        {
            RedIcon icon = new RedIcon();

            icon.SetBinding( RedIcon.CanvasProperty, new Binding( "Canvas" ) { Source = this } );
            Binding sizeBinding = new Binding("IconSize") { Source = this };

            icon.SetBinding( RedIcon.WidthProperty, sizeBinding );
            icon.SetBinding( RedIcon.HeightProperty, sizeBinding );

            Setter setter = new Setter( MenuItem.IconProperty, icon );
            Setters.Add( setter );
        }
    }
}
